package com.easpeed.comon.utils

import android.os.Handler
import android.os.Looper
import androidx.core.util.Consumer
import com.easpeed.common.CommonConsts
import com.easpeed.comon.bean.BaseData
import com.easpeed.comon.bean.LoginResult
import com.easpeed.comon.bean.Version
import com.easpeed.comon.consts.ConstsApi
import com.google.gson.reflect.TypeToken
import java.io.File

/**
 * Created by bigyu on 2025-01-24 14:14
 * com.easpeed.comon.utils
 */
object ApiHelper {


    @JvmStatic
    fun checkVersion(consumer: Consumer<BaseData<Version>>) {
        OkHttpHelper.get(String.format(ConstsApi.api_version, CommonConsts.appType.toString()), callback = { success, response ->
            com.bigyu.utils.Logger.e("TAG", "checkVersion: success=$success, response=$response")
            try {
                val type = object : TypeToken<BaseData<Version>>() {}.type
                val data = GsonUtils.parseType<BaseData<Version>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }

            }
        })
    }

    @JvmStatic
    fun login(map: HashMap<String, Any>, consumer: Consumer<BaseData<LoginResult>>) {
        OkHttpHelper.post(String.format(ConstsApi.api_login), body = map, callback = { success, response ->
            com.bigyu.utils.Logger.e("TAG", "checkVersion: success=$success, response=$response")
            try {
                val type = object : TypeToken<BaseData<LoginResult>>() {}.type
                val data = GsonUtils.parseType<BaseData<LoginResult>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun createLine(map: HashMap<String, Any>, consumer: Consumer<BaseData<Line>>) {
        OkHttpHelper.post(String.format(ConstsApi.api_createLine), body = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<Line>>() {}.type
                val data = GsonUtils.parseType<BaseData<Line>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun waveLength(map: HashMap<String, Any>, consumer: Consumer<BaseData<List<String>>>) {
        OkHttpHelper.get(String.format(ConstsApi.api_waveLength), params = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<List<String>>>() {}.type
                val data = GsonUtils.parseType<BaseData<List<String>>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun createRecord(map: HashMap<String, Any?>, consumer: Consumer<BaseData<Record>>) {
        OkHttpHelper.post(String.format(ConstsApi.api_createRecord), body = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<Record>>() {}.type
                val data = GsonUtils.parseType<BaseData<Record>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun updateRecord(map: HashMap<String, Any?>, consumer: Consumer<BaseData<String>>) {
        OkHttpHelper.post(String.format(ConstsApi.api_updateRecord), body = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<String>>() {}.type
                val data = GsonUtils.parseType<BaseData<String>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }
    @JvmStatic
    fun uploadLocation(map: HashMap<String, Any?>, consumer: Consumer<BaseData<String>>) {
        OkHttpHelper.post(String.format(ConstsApi.api_uploadLocation), body = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<String>>() {}.type
                val data = GsonUtils.parseType<BaseData<String>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun lineList(map: HashMap<String, Any>, consumer: Consumer<BaseData<List<Line>>>) {
        OkHttpHelper.get(String.format(ConstsApi.api_lineList), params = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<List<Line>>>() {}.type
                val data = GsonUtils.parseType<BaseData<List<Line>>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun recordList(id: String, map: HashMap<String, Any>, consumer: Consumer<BaseData<List<Record>>>) {
        OkHttpHelper.get(String.format(ConstsApi.api_listRecord, id), params = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<List<Record>>>() {}.type
                val data = GsonUtils.parseType<BaseData<List<Record>>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun deleteLine(lineId: String?, map: HashMap<String, Any>, consumer: Consumer<BaseData<String>>) {
        OkHttpHelper.get(String.format(ConstsApi.api_delete, lineId), params = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<String>>() {}.type
                val data = GsonUtils.parseType<BaseData<String>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun deleteRecord(lineId: String?, map: HashMap<String, Any?>, consumer: Consumer<BaseData<String>>) {
        OkHttpHelper.get(String.format(ConstsApi.api_deleteRecord, lineId), params = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<String>>() {}.type
                val data = GsonUtils.parseType<BaseData<String>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun deleteOptical(lineId: String?, map: HashMap<String, Any>, consumer: Consumer<BaseData<String>>) {
        OkHttpHelper.get(String.format(ConstsApi.api_deleteOptical, lineId), params = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<String>>() {}.type
                val data = GsonUtils.parseType<BaseData<String>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun updateLine(map: HashMap<String, Any>, consumer: Consumer<BaseData<String>>) {
        OkHttpHelper.post(String.format(ConstsApi.api_updateLine), body = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<String>>() {}.type
                val data = GsonUtils.parseType<BaseData<String>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun updateOptical(map: HashMap<String, Any>, consumer: Consumer<BaseData<Optical>>) {
        OkHttpHelper.post(String.format(ConstsApi.api_updateOptical), body = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<Optical>>() {}.type
                val data = GsonUtils.parseType<BaseData<Optical>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun createOptical(map: HashMap<String, Any?>, consumer: Consumer<BaseData<Optical>>) {
        OkHttpHelper.post(String.format(ConstsApi.api_createOptical), body = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<Optical>>() {}.type
                val data = GsonUtils.parseType<BaseData<Optical>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }


    @JvmStatic
    fun opticalList(map: HashMap<String, Any>, consumer: Consumer<BaseData<List<Optical>>>) {
        OkHttpHelper.get(String.format(ConstsApi.api_lineOptical), params = map, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<List<Optical>>>() {}.type
                val data = GsonUtils.parseType<BaseData<List<Optical>>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }

    @JvmStatic
    fun uploadFile(file: File, type: String = "1" , consumer: Consumer<BaseData<UploadFile>>) {
        OkHttpHelper.uploadFile(String.format(ConstsApi.api_fileUpload), file, type, callback = { success, response ->
            try {
                val type = object : TypeToken<BaseData<UploadFile>>() {}.type
                val data = GsonUtils.parseType<BaseData<UploadFile>>(response, type)
                    ?: throw Exception("服务器异常")
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(data)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Handler(Looper.getMainLooper()).post {
                    consumer.accept(BaseData(null, code = -1, message = e.message))
                }
            }
        })
    }


}